//
//  welcomeViewController.swift
//  XJWeiBo
//
//  Created by 徐健 on 16/3/14.
//  Copyright © 2016年 徐健. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class welcomeViewController: UIViewController {
    
    
    override func loadView() {
        view = backImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setUpUI()
    }
    
    
    
    //界面已经出现的时候会自动调用,在这个方法中所有的子视图的位置 已经设置完毕
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    
    
    
    
    
    private func setUpUI(){
        view.addSubview(iconImageView)
        
        view.addSubview(welcomeLable)
        
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            
            
            //大小的约束
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        welcomeLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
            
            make.centerX.equalTo(iconImageView.snp_centerX)
        }
        
        //设置图片
        iconImageView.sd_setImageWithURL(userAccount.onlyUserAccount.headImageURL)
        
        
        //设置layer的属性
        iconImageView.layer.cornerRadius = 45
        
        iconImageView.layer.masksToBounds = true
        
        iconImageView.layer.borderWidth = 3
        
        iconImageView.layer.borderColor = UIColor.purpleColor().CGColor
        
        
        welcomeLable.alpha = 0
        
        
    }
    
    
    
    
    private lazy var backImageView = UIImageView(image:UIImage(named: "ad_background"))
    
    private lazy var iconImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var welcomeLable:UILabel = UIFactory.lableFactory(UIColor.brownColor(), text: (userAccount.onlyUserAccount.name ?? "") + "欢迎归来", fontSize: 16)

    
    
    
    private func startAnimation(){
        let y = -UIScreen.mainScreen().bounds.height + 180
        
        
        
        
        //只收集了约束变化，没改变frame
        self.iconImageView.snp_updateConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(y)
        })
        
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity:9.8, options:[], animations: { () -> Void in
           
            //强制刷新
            self.view.layoutIfNeeded()
            
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLable.alpha = 1
                    }, completion: { (_) -> Void in
                        print("ok")
                        
                        //切换根视图控制器
//                        UIApplication.sharedApplication().keyWindow?.rootViewController = MainViewController()
                        
                        
                        // 发布通知
                        NSNotificationCenter.defaultCenter().postNotificationName(switchRootViewController, object: nil)
                        
                })
                
        }
    }



}
